from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.CourseAdminController import CourseAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import QueryBase, SubjectSectionQuery
from schema.TeachingSchema import SubjectInfo, CourseStageSubjectInfo, CourseStageInfo, SubjectSectionInfo, CourseInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

course_admin_router = APIRouter()


@course_admin_router.post("/course/add", summary='添加课程信息')
def add_course(courseInfo: CourseInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/course/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/add', db):
            response = CourseAdminController.add_course(courseInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/course/mod", summary='根据课程id修改课程信息')
def mod_course(courseInfo: CourseInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/course/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/mod', db):
            response = CourseAdminController.mod_course(courseInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/course/queryall", summary='查询所有课程信息')
def query_course_all(db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/course/queryall')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/query', db):
            response = CourseAdminController.query_course_all(db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result

@course_admin_router.post("/course/querybypage", summary='分页查询所有课程信息')
def query_course_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/course/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/query', db):
            response = CourseAdminController.query_course_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result



@course_admin_router.post("/subject/add", summary='添加科目知识点信息')
def add_subject(subjectInfo: SubjectInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subject/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/add', db):
            response = CourseAdminController.add_subject(subjectInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/subject/mod", summary='根据科目知识点id修改科目知识点信息')
def mod_subject(subjectInfo: SubjectInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subject/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/mod', db):
            response = CourseAdminController.mod_subject(subjectInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/subjectsection/add", summary='添加科目知识点小节信息')
def add_subject_section(subjectSectionInfo: SubjectSectionInfo, db: Session = Depends(get_db_session),
                        result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subjectsection/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/add', db):
            response = CourseAdminController.add_subject_section(subjectSectionInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/subjectsection/mod", summary='根据subject_id和section_id修改科目知识点小节信息')
def mod_subject_section(subjectSectionInfo: SubjectSectionInfo, db: Session = Depends(get_db_session),
                        result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subjectsection/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/mod', db):
            response = CourseAdminController.mod_subject_section(subjectSectionInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/subject/querybypage", summary='分页查询所有科目知识点信息')
def query_subject_by_page(query_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subject/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/query', db):
            response = CourseAdminController.query_subject_by_page(query_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/subjectsection/query", summary='根据subject_id、section_id查询科目知识点小节信息')
def query_subject_section_by_field(subjectSectionQuery: SubjectSectionQuery, db: Session = Depends(get_db_session),
                                   result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/subjectsection/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/query', db):
            response = CourseAdminController.query_subject_section_by_field(subjectSectionQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.delete("/subjectsection/delete/{subject_id}/{section_id}", summary='根据subject_id、section_id删除科目知识点小节信息')
def delete_subject_section(subject_id: int, section_id: int, db: Session = Depends(get_db_session),
                           result=Depends(check_token)):
    Log.getlogger().debug(
        f'recieved request:/subjectsection/delete/subject_id={subject_id}&section_id={section_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/subject/mod', db):
            response = CourseAdminController.delete_subject_section(subject_id, section_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/coursestage/add", summary='添加课程阶段信息')
def add_course_stage(courseStageInfo: CourseStageInfo, db: Session = Depends(get_db_session),
                     result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/add', db):
            response = CourseAdminController.add_course_stage(courseStageInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/coursestage/mod", summary='根据课程阶段id修改课程阶段信息')
def mod_course_stage(courseStageInfo: CourseStageInfo, db: Session = Depends(get_db_session),
                     result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/mod', db):
            response = CourseAdminController.mod_course_stage(courseStageInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.get("/coursestage/query/{course_content_id}", summary='根据课程大纲id查询课程阶段信息')
def query_course_stage_by_field(course_content_id: int, db: Session = Depends(get_db_session),
                                result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/query/course_content_id={course_content_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/query', db):
            response = CourseAdminController.query_course_stage_by_field(course_content_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.delete("/coursestage/delete/{course_stage_id}", summary='根据课程阶段id删除课程阶段信息')
def delete_course_stage(course_stage_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/delete/course_stage_id={course_stage_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/mod', db):
            response = CourseAdminController.delete_course_stage(course_stage_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/coursestage/subject/add", summary='添加课程阶段科目明细信息表')
def add_course_stage_subject(courseStageSubjectInfo: CourseStageSubjectInfo, db: Session = Depends(get_db_session),
                             result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/subject/add')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/add', db):
            response = CourseAdminController.add_course_stage_subject(courseStageSubjectInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.post("/coursestage/subject/mod", summary='根据课程阶段科目明细id修改课程阶段科目明细信息')
def mod_course_stage_subject(courseStageSubjectInfo: CourseStageSubjectInfo, db: Session = Depends(get_db_session),
                             result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/subject/mod')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/mod', db):
            response = CourseAdminController.mod_course_stage_subject(courseStageSubjectInfo, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.get("/coursestage/subject/query/{course_stage_id}", summary='根据课程阶段id查询课程阶段科目明细信息')
def query_course_stage_subject_by_field(course_stage_id: int, db: Session = Depends(get_db_session),
                                        result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/coursestage/subject/query/course_stage_id={course_stage_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/query', db):
            response = CourseAdminController.query_course_stage_subject_by_field(course_stage_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@course_admin_router.delete("/coursestage/subject/delete/{course_stage_id}/{subject_id}",
                              summary='根据课程阶段id和科目知识点id删除课程阶段科目明细信息')
def delete_course_stage_subject(course_stage_id: int, subject_id: int, db: Session = Depends(get_db_session),
                                result=Depends(check_token)):
    Log.getlogger().debug(
        f'recieved request:/coursestage/subject/delete/course_stage_id={course_stage_id}&subject_id={subject_id}')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/course/mod', db):
            response = CourseAdminController.delete_course_stage_subject(course_stage_id, subject_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
